<?php

class Sondage_MonsondageController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	parent::init();
    	$this->_helper->layout()->disableLayout();  	
    	if (Zend_Auth::getInstance()->hasIdentity()) {
    		//echo "already logged in as: " ;//. Zend_Auth::getInstance()->getIdentity();
    		$identity=Zend_Auth::getInstance()->getIdentity();
    		
    		if($identity->role!="admin"){
    			$this->_helper->layout()->setLayout("utilisateurlayout");
    		} else{
    			$this->_helper->layout()->setLayout("layout");
    		}
    		
    		$this->view->user=$identity->nom." ".$identity->prenom;
    		
    		
    	}else{
    		$this->_helper->redirector('login',"authentification","sondage");
    	}
    	
    }

    public function indexAction()
    {
        // action body
        
    	//chargement des donnée de la base de donnée
    	$mapper = new Application_Model_Mapper_Sondage();
    	 
    	$header=array(
    			"titre"	=> "Titre"
    			,"description"	=> "Description"
    			,"domaine_iddomaine" => "Domaine"
    			,"score" => "Score"
    	);
    	 
    	$this->view->headercolums=$header;
    	$identity=Zend_Auth::getInstance()->getIdentity();
    	
    	$select=$mapper->getSondageParticiper($identity->idsondagepersonne);
     
    	$this->view->paginator = $select;
    	
    
    	
    }

    public function repondreAction()
    {
        // action body
        
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->layout()->setLayout("reponselayout");
    	
    	
    	$identity=Zend_Auth::getInstance()->getIdentity();
    	$idsondage=$this->getRequest()->getParam("id");
    	
    	$participantdb=new Application_Model_DbTable_Participation();
    	$res=$participantdb->fetchRow("  sondagepersonne_idsondagepersonne = ".$identity->idsondagepersonne." AND sondage_idsondage= $idsondage");
    	
    	
    	$part=new Application_Model_Mapper_Sondage();
    	$respart=$part->ispasser($identity->idsondagepersonne, $idsondage);
    	if(count($respart)>0){
    		
    		//$this->_helper->redirector("index","monsondage","sondage");
    	}
    	$reponsesforme=new Application_Form_Mesreponses();
    	$this->view->form=$reponsesforme;
    	
   	if ($this->_request->isPost()) {
    		$formData = $this->_request->getPost();
    		
    		if ($reponsesforme->isValid($formData)) {
    			$valrep=0;
    			$reponse = new Application_Model_DbTable_Reponseparticipant();
    			
    			$participant=new Application_Model_Mapper_Participation();
    			
    			
    			$particip=$participant->getPaticipant($identity->idsondagepersonne, $idsondage);
    			
    			$row = $reponse->createRow();
    			$date=new Zend_Date();
    		
    			$row->datereponse = $date->get(Zend_Date::TIMESTAMP);
    		 
    			$row->score = $reponsesforme->getValue('textquestion');
    			$row->participation_idparticipation = $particip['idparticipation'];
    			
    			$result = $row->save();
    			$listreponse = new Application_Model_DbTable_Listesreponse();

    	 		 if($result){
    			 	$score=0;
    			 	
	    			foreach($formData as $key => $value){
	    				
	    				$valkey=explode("_", $key);
	    				if($valkey[0]=="Question"){
	    					
	    					$row0 = $listreponse->createRow();
	    					$valrep=explode("_", $value);
	    					$row0->valuereponse=$valrep[0];
	    					$row0->reponseparticipant_idreponseparticipant = $result;
	    					$row0->Question_idQuestion = $valkey[1];
	    					$result0 = $row0->save();
	    					$score+=$valrep[1];
	    				}
	    			}
	    			
	    			print_r($particip);
	    			$reponse->update(array("score"=>$score), "idreponseparticipant= $result");
	    			$participantdb=new Application_Model_dbTable_Participation();
	    			$participantdb->update(array("scorefinal"=>$score), "idparticipation= ".$particip['idparticipation']);
	    			
    			 }
    	
    			$this->_helper->redirector("index","monsondage","sondage");
    		}
    		 
    	}
    	
    	
    	
    	
    }

    public function inscriptionsondageAction()
    {
        // action body
        
    	$identity=Zend_Auth::getInstance()->getIdentity();
   	 
    	$participant=new Application_Model_DbTable_Participation();
    	
    	$row = $participant->createRow();
    	$row->sondage_idsondage = $this->_request->getParam("id");
    	$row->sondagepersonne_idsondagepersonne = $identity->idsondagepersonne;
    	 
    	$result = $row->save();
    	
    	$this->_helper->redirector("repondre","Monsondage","sondage",array("id"=>$this->_request->getParam("id")));
    	 
    }

    public function monscoreAction()
    {
        // action body
    }


}







